//
//  NewFeatureViewController.swift
//  WKweiBo
//
//  Created by 阿拉斯加的狗 on 15/12/9.
//  Copyright © 2015年 阿拉斯加的🐶. All rights reserved.
//

import UIKit
import SnapKit
//定义可重用Cellid
private let WKNewFeatureViewCellID = "WKNewFeatureViewCellID"

//新特性图像的数量
private let WKNewFeatureViewCount = 4

class NewFeatureViewController: UICollectionViewController {

    
    init() {
    
        //创建一个UICollectionViewFlowLayout
         let layout = UICollectionViewFlowLayout()
        
        //创建cell大小及属性
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        //设置滚动的方向
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
    
        //设置collectionView的属性
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: WKNewFeatureViewCellID)


    }

    //隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        
        return true
        
    }

    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return WKNewFeatureViewCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WKNewFeatureViewCellID, forIndexPath: indexPath) as! NewFeatureCell
    
        //设置颜色
        cell.backgroundColor = UIColor.blueColor()
        
        cell.imageIndex = indexPath.item
    
        return cell
    }

    
    //当图片滚动到最后一面要显示按钮的动画效果
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = Int (scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page != WKNewFeatureViewCount - 1 {
        
            return
        
        }
        
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
        
        //显示动画
        cell.showButtonAnim()
        
    }
    
    
}

/// 新特性cell
private class NewFeatureCell: UICollectionViewCell {
    
    //设置cell的大小
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK - 懒加载控件
    private lazy var iconView: UIImageView = UIImageView()
    
    private lazy var startButton: UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), imageName: "new_feature_finish_button")
    
    
    
    //设置图片的属性
    private var imageIndex: Int = 0 {
    
        didSet{
        
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
        
            //隐藏按钮
            self.startButton.hidden = true
            
        }
    
    }
    
    //显示按钮动画
    private func showButtonAnim() {
    
            //显示按钮动画
            self.startButton.hidden = false
        
                //禁止交互
                self.startButton.userInteractionEnabled = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        
        UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
                //允许交互
                self.startButton.userInteractionEnabled = true
                
        }
    
    
    
    }
    
    //监听按钮点击事件
    @objc private func clickButton() {
    
        print("进行点击")
        
        NSNotificationCenter.defaultCenter().postNotificationName(WKSwitchRootViewControllerNotification, object: nil)
    
    }
    
    
    //添加控件到cell中
    private func setUpUI() {
    
        addSubview(iconView)
    
        addSubview(startButton)
        
        //设置frame
        iconView.frame = bounds
        
        //设置按钮的自动布局
        startButton.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
            
        }
        
        //按钮点击事件
        startButton.addTarget(self, action: "clickButton", forControlEvents: .TouchUpInside)
    
    }
    

}

